package cn.hpclub.server.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class BeanConverter{

    public static Map<String, Object> getQueryParams(HttpServletRequest request){
        Map<String, String[]> map = new HashMap<String, String[]>();
        if(request.getMethod().equalsIgnoreCase("POST")){
            map = request.getParameterMap();
        } 

        Map<String, Object> params = new HashMap<String, Object>(map.size());
        int len;
        for(Map.Entry<String, String[]> entry : map.entrySet()){
            len = entry.getValue().length;
            if(len == 1){
                params.put(entry.getKey(), entry.getValue()[0]);
            } else if(len > 1){
                params.put(entry.getKey(), entry.getValue());
            }
        }
        return params;
    }

    public static Object toJavaBean(Object javabean, Map<String, String[]> m){
        // 所有声明了的方法
        Method[] methods = javabean.getClass().getDeclaredMethods();
        for(Method method : methods){
            if(method.getName().startsWith("set")){
                // 得到该方法的参数类型
                Class<?>[] params = method.getParameterTypes();
                String field = method.getName();
                field = field.substring(field.indexOf("set") + 3);
                field = field.toLowerCase().charAt(0) + field.substring(1);
                Object value = m.get(field.toString());
                try{
                    // convert types for some popular ones
                    if(value != null && !"".equals(value)){
                        String pa = params[0].getName().toString();
                        // if (value instanceof java.util.Date) {
                        if(pa.equals("java.util.Date")){
                            value = new java.util.Date(((java.util.Date)value).getTime());
                        } else if(pa.equals("java.lang.String")){
                            value = new java.lang.String(value.toString());
                        } else if(pa.equals("java.lang.Integer") || pa.equals("int")){
                            value = new java.lang.Integer(value.toString());
                        } else if(pa.equals("java.lang.Long")){
                            value = new java.lang.Long(value.toString());
                        } else if(pa.equals("java.lang.Double")){
                            value = new java.lang.Double(value.toString());
                        } else if(pa.equals("java.lang.Float")){
                            value = new java.lang.Float(value.toString());
                        } else if(pa.equals("java.lang.Short")){
                            value = new java.lang.Short(value.toString());
                        } else if(pa.equals("java.lang.Byte")){
                            value = new java.lang.Byte(value.toString());
                        } else if(pa.equals("java.lang.Character")){
                            // value = new java.lang.Character(value);
                        } else if(pa.equals("java.lang.Boolean")){
                            value = new java.lang.Boolean(value.toString());
                        }

                        method.invoke(javabean, m.get(field.toString()));
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        return javabean;
    }
}
